﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JYM.Common
{
    /// <summary>
    /// WEB页面上的页面消息提示工具类
    /// </summary>
    public class MessageBox
    {
        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(Page page, string msg)
        {
            ResponseScript(page, "message", "alert('" + msg.ToString() + "');");
        }

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="Control">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(WebControl Control, string msg)
        {
            //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("this.location.href='{0}'", url);
            ResponseScript(page,"message", Builder.ToString());

        }
        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="key">要注册的启动脚本的键</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(Page page,string key, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), key, "<script language='javascript' defer>" + script + "</script>");

        }
    }
}
